<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'tb_controlview', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "tb_controlviewinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "tb_usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$tb_controlview->Export = @$_GET["export"]; // Get export parameter
$sExport = $tb_controlview->Export; // Get export parameter, used in header
$sExportFile = $tb_controlview->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["ID"] <> "") {
	$tb_controlview->ID->setQueryStringValue($_GET["ID"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$tb_controlview->CurrentAction = $_POST["a_edit"]; // Get action code
	LoadFormValues(); // Get form values
} else {
	$tb_controlview->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($tb_controlview->ID->CurrentValue == "") Page_Terminate($tb_controlview->getReturnUrl()); // Invalid key, exit
switch ($tb_controlview->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($tb_controlview->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$tb_controlview->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($tb_controlview->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$tb_controlview->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_ControlDate"];
		if (elm && !ew_CheckDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = yyyy/mm/dd - Control Date"))
				return false; 
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: Control View<br><br><a href="<?php echo $tb_controlview->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="ftb_controlviewedit" id="ftb_controlviewedit" action="tb_controlviewedit.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">ID</td>
		<td<?php echo $tb_controlview->ID->CellAttributes() ?>><span id="cb_x_ID">
<div<?php echo $tb_controlview->ID->ViewAttributes() ?>><?php echo $tb_controlview->ID->EditValue ?></div>
<input type="hidden" name="x_ID" id="x_ID" value="<?php echo ew_HtmlEncode($tb_controlview->ID->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Control Date</td>
		<td<?php echo $tb_controlview->ControlDate->CellAttributes() ?>><span id="cb_x_ControlDate">
<input type="text" name="x_ControlDate" id="x_ControlDate" title="" value="<?php echo $tb_controlview->ControlDate->EditValue ?>"<?php echo $tb_controlview->ControlDate->EditAttributes() ?>>
&nbsp;<img src="images/ew_calendar.gif" id="cx_ControlDate" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_ControlDate", // ID of the input field
ifFormat : "%Y/%m/%d", // the date format
button : "cx_ControlDate" // ID of the button
}
);
</script>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Status</td>
		<td<?php echo $tb_controlview->Status->CellAttributes() ?>><span id="cb_x_Status">
<?php
$arwrk = $tb_controlview->Status->EditValue;
if (is_array($arwrk)) {
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($tb_controlview->Status->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " checked" : "";
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="radio" name="x_Status" id="x_Status" title="" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php $tb_controlview->Status->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="   Edit   ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $tb_controlview;
	$tb_controlview->ID->setFormValue($objForm->GetValue("x_ID"));
	$tb_controlview->ControlDate->setFormValue($objForm->GetValue("x_ControlDate"));
	$tb_controlview->ControlDate->CurrentValue = ew_UnFormatDateTime($tb_controlview->ControlDate->CurrentValue, 5);
	$tb_controlview->Status->setFormValue($objForm->GetValue("x_Status"));
}

// Restore form values
function RestoreFormValues() {
	global $tb_controlview;
	$tb_controlview->ID->CurrentValue = $tb_controlview->ID->FormValue;
	$tb_controlview->ControlDate->CurrentValue = $tb_controlview->ControlDate->FormValue;
	$tb_controlview->ControlDate->CurrentValue = ew_UnFormatDateTime($tb_controlview->ControlDate->CurrentValue, 5);
	$tb_controlview->Status->CurrentValue = $tb_controlview->Status->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $tb_controlview;
	$sFilter = $tb_controlview->SqlKeyFilter();
	if (!is_numeric($tb_controlview->ID->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@ID@", ew_AdjustSql($tb_controlview->ID->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$tb_controlview->Row_Selecting($sFilter);

	// Load sql based on filter
	$tb_controlview->CurrentFilter = $sFilter;
	$sSql = $tb_controlview->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$tb_controlview->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $tb_controlview;
	$tb_controlview->ID->setDbValue($rs->fields('ID'));
	$tb_controlview->ControlDate->setDbValue($rs->fields('ControlDate'));
	$tb_controlview->Status->setDbValue($rs->fields('Status'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $tb_controlview;

	// Call Row Rendering event
	$tb_controlview->Row_Rendering();

	// Common render codes for all row types
	// ID

	$tb_controlview->ID->CellCssStyle = "";
	$tb_controlview->ID->CellCssClass = "";

	// ControlDate
	$tb_controlview->ControlDate->CellCssStyle = "";
	$tb_controlview->ControlDate->CellCssClass = "";

	// Status
	$tb_controlview->Status->CellCssStyle = "";
	$tb_controlview->Status->CellCssClass = "";
	if ($tb_controlview->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($tb_controlview->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($tb_controlview->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// ID
		$tb_controlview->ID->EditCustomAttributes = "";
		$tb_controlview->ID->EditValue = $tb_controlview->ID->CurrentValue;
		$tb_controlview->ID->CssStyle = "";
		$tb_controlview->ID->CssClass = "";
		$tb_controlview->ID->ViewCustomAttributes = "";

		// ControlDate
		$tb_controlview->ControlDate->EditCustomAttributes = "";
		$tb_controlview->ControlDate->EditValue = ew_FormatDateTime($tb_controlview->ControlDate->CurrentValue, 5);

		// Status
		$tb_controlview->Status->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Enable");
		$arwrk[] = array("0", "Disable");
		$tb_controlview->Status->EditValue = $arwrk;
	} elseif ($tb_controlview->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$tb_controlview->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $tb_controlview;
	$sFilter = $tb_controlview->SqlKeyFilter();
	if (!is_numeric($tb_controlview->ID->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@ID@", ew_AdjustSql($tb_controlview->ID->CurrentValue), $sFilter); // Replace key value
	$tb_controlview->CurrentFilter = $sFilter;
	$sSql = $tb_controlview->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field ID
		// Field ControlDate

		$tb_controlview->ControlDate->SetDbValueDef(ew_UnFormatDateTime($tb_controlview->ControlDate->CurrentValue, 5), NULL);
		$rsnew['ControlDate'] =& $tb_controlview->ControlDate->DbValue;

		// Field Status
		$tb_controlview->Status->SetDbValueDef($tb_controlview->Status->CurrentValue, NULL);
		$rsnew['Status'] =& $tb_controlview->Status->DbValue;

		// Call Row Updating event
		$bUpdateRow = $tb_controlview->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($tb_controlview->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($tb_controlview->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $tb_controlview->CancelMessage;
				$tb_controlview->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$tb_controlview->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
